
package com.axc.gallery2.camera.activity;

import java.io.File;
import java.text.SimpleDateFormat;

import android.app.Activity;
import android.content.Intent;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.net.Uri;
import android.os.Bundle;
import android.os.Environment;
import android.view.Display;
import android.view.Menu;
import android.view.MenuItem;
import android.view.SubMenu;
import android.view.Window;
import android.widget.ImageView;
import android.widget.Toast;

import com.axc.gallery2.R;
import com.axc.gallery2.dbhelper.ImageInfo;
import com.axc.gallery2.edit.EditActivity;
import com.axc.gallery2.net.activity.UploadPhotoActivity;
import com.renren.api.connect.android.Renren;

/**
 * 调用系统照相机类
 *
 */
public class CameraResultactivity extends Activity {

    String imageFilePath;// 图片存入地址
    ImageView imv;
    private static final int REQUEST_CAMERA = 1;
    
    private Renren renren;

  //主题应用
	public static CameraResultactivity Talbum;
	public int theme = -1;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        // 主题
        Talbum = this;
        theme = getSharedPreferences("cons", MODE_PRIVATE).getInt("theme",
                android.R.style.Theme);
        setTheme(theme);
        super.onCreate(savedInstanceState);

        // 设置不显示标题栏
        requestWindowFeature(Window.FEATURE_NO_TITLE);
        setContentView(R.layout.camera_result);

        // 图片存入地址
        SimpleDateFormat format = new SimpleDateFormat("yyyymmdd_HHmmss");
        String time = format.format(new java.util.Date());

        imageFilePath = Environment.getExternalStorageDirectory().getAbsolutePath()
                + "/DCIM/Camera/Gallery2_" + time + ".jpg";

        File imageFile = new File(imageFilePath);

        Uri imageFileUri = Uri.fromFile(imageFile);

        Intent i = new Intent(android.provider.MediaStore.ACTION_IMAGE_CAPTURE);

        i.putExtra(android.provider.MediaStore.EXTRA_OUTPUT, imageFileUri);

        startActivityForResult(i, REQUEST_CAMERA);

    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        // TODO Auto-generated method stub
        super.onActivityResult(requestCode, resultCode, data);

        // 如果拍照成功

        if (resultCode == RESULT_OK) {

            // Bundle extras = intent.getExtras（）；

            // Bitmap bmp = （Bitmap）extras.get（"data"）；

            imv = (ImageView) findViewById(R.id.CameraImg);

            // 取得屏幕的显示大小
            Display currentDisplay = getWindowManager().getDefaultDisplay();
            int dw = currentDisplay.getWidth();
            int dh = currentDisplay.getHeight();

            // 对拍出的照片进行缩放
            BitmapFactory.Options bmpFactoryOptions = new BitmapFactory.Options();
            bmpFactoryOptions.inJustDecodeBounds = true;
            Bitmap bmp = BitmapFactory.decodeFile(imageFilePath, bmpFactoryOptions);
            int heightRatio = (int) Math.ceil(bmpFactoryOptions.outHeight / (float) dh);
            int widthRatio = (int) Math.ceil(bmpFactoryOptions.outWidth / (float) dw);
            if (heightRatio > 1 && widthRatio > 1) {

                if (heightRatio > widthRatio) {

                    bmpFactoryOptions.inSampleSize = heightRatio;

                } else {

                    bmpFactoryOptions.inSampleSize = widthRatio;

                }

            }

            bmpFactoryOptions.inJustDecodeBounds = false;

            bmp = BitmapFactory.decodeFile(imageFilePath, bmpFactoryOptions);

            imv.setImageBitmap(bmp);
        }

    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        int base = Menu.FIRST;
        // 一个menu可以包括多个子菜单
        SubMenu subMenu = menu.addSubMenu(base, 11, Menu.NONE, R.string.camera_menu_title);
        // 子菜单可以包括多个菜单项
        subMenu.add(base, 0, base + 1, R.string.camera_menu_opera1);
        subMenu.add(base, 1, base + 2, R.string.camera_menu_opera2);
        subMenu.add(base, 2, base + 3, R.string.camera_menu_opera3);
        return true;
    }

    // 设置按钮（主题）
    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        Intent intent = new Intent();
        Bundle bundle = new Bundle();
        switch (item.getItemId()) {
            
            case 0:
                // Toast.makeText(CameraResultactivity.this, "0",
                // Toast.LENGTH_SHORT).show();
                intent.setClass(getBaseContext(), EditActivity.class);
                bundle.putString("pp", imageFilePath);
                intent.putExtra("data", bundle);
                CameraResultactivity.this.startActivity(intent);

                break;
            case 1:
                //Toast.makeText(CameraResultactivity.this, "1", Toast.LENGTH_SHORT).show();
                //renren.authorize(NetAlbumsActivity.this, listener);
                intent.setClass(getBaseContext(), UploadPhotoActivity.class);
                bundle.putString(ImageInfo.IAMGEPATH, imageFilePath);
                intent.putExtra("data", bundle);
                intent.putExtra(Renren.RENREN_LABEL, renren);
                intent.putExtra("file", new File(imageFilePath));
                CameraResultactivity.this.startActivity(intent);
                break;
            case 2:
                Toast.makeText(CameraResultactivity.this, "2", Toast.LENGTH_SHORT).show();
                break;
        }
        return false;
    }

}
